package com.daniel.cms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daniel.cms.entity.AccountEntity;
import com.daniel.cms.mapper.AccountMapper;
import com.daniel.cms.utils.JwtUtil;
import com.daniel.cms.utils.MapUtil;
import com.daniel.cms.utils.Mes;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 详细说明
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/4/21 10:49
 */
@Slf4j
@Service
public class AccountService {
	@Autowired
	AccountMapper mapper;

	/**
	 * 登录
	 *
	 * @param map 请求实体
	 * @return Mes
	 */
	public Mes login(Map<String, String> map) {
		try {
			String account = map.get("username");
			if (StringUtils.isBlank(account)) {
				return Mes.error("请求登录账号为空");
			}
			String password = map.get("password");
			if (StringUtils.isBlank(password)) {
				return Mes.error("请求登录密码为空");
			}
			QueryWrapper<AccountEntity> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("account", account);
			List<AccountEntity> list = mapper.selectList(queryWrapper);
			if (list != null && list.size() > 0) {
				AccountEntity model = list.get(0);
				if (!model.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))) {
					return Mes.error("用户密码不正确");
				}
				// 生成token
				String token = JwtUtil.sign(account, model.getId() + "");
				if (token != null) {
					return Mes.success(token);
				} else {
					return Mes.error("token生成失败,请重新登录");
				}
			} else {
				return new Mes(2, "用户不存在", new HashMap<>(0));
			}
		} catch (Exception ex) {
			return Mes.error(ex.getMessage());
		}
	}

	/**
	 * 根据token获取用户信息
	 *
	 * @param token token
	 * @return Mes
	 */
	public Mes info(String token) {
		try {
			if (StringUtils.isBlank(token)) {
				return Mes.error("请求token为空");
			}
			String code = JwtUtil.getUserCode(token);
			if (!StringUtils.isBlank(code)) {
				AccountEntity entity = mapper.selectById(code);
				String[] roles = entity.getRoles().split(",");
				Map<String, Object> map = MapUtil.objToMap(entity);
				map.put("roles", roles);
				return entity != null ? Mes.success(map) : Mes.error("用户不存在");
			} else {
				return Mes.error("签名信息校验失败,请重新登录");
			}
		} catch (Exception ex) {
			return Mes.error(ex.getMessage());
		}
	}

	/**
	 * 创建用户
	 *
	 * @param entity 请求实体
	 * @return Mes
	 */
	public Mes create(AccountEntity entity) {
		try {
			entity.setPassword(DigestUtils.md5DigestAsHex(entity.getRule().getBytes()));
			int result = mapper.insert(entity);
			return result > 0 ? Mes.success("新增成功") : Mes.error("新增失败");
		} catch (Exception ex) {
			return Mes.error(ex.getMessage());
		}
	}

	/**
	 * 更新用户
	 *
	 * @param entity 请求实体
	 * @return Mes
	 */
	public Mes update(AccountEntity entity) {
		try {
			AccountEntity model = mapper.selectById(entity.getId());
			if (model != null) {
				model.setAccount(entity.getAccount())
						.setName(entity.getName())
						.setPhone(entity.getPhone())
						.setRule(entity.getRule())
						.setPassword(DigestUtils.md5DigestAsHex(entity.getRule().getBytes()))
						.setEmail(entity.getEmail());
				int result = mapper.updateById(model);
				return result > 0 ? Mes.success("更新成功") : Mes.error("更新失败");
			} else {
				return Mes.error("数据不存在");
			}
		} catch (Exception ex) {
			return Mes.error(ex.getMessage());
		}
	}

	/**
	 * 删除用户
	 *
	 * @param map Map
	 * @return Mes
	 */
	public Mes delete(Map<String, String> map) {
		try {
			String ids = map.get("ids");
			String[] stars = ids.split(",");
			int result = mapper.deleteBatchIds(Arrays.asList(stars));
			return result > 0 ? Mes.success("删除成功") : Mes.error("删除失败");
		} catch (Exception ex) {
			return Mes.error(ex.getMessage());
		}
	}

	/**
	 * 获取数据
	 *
	 * @param current 页索引
	 * @param limit   页大小
	 * @param name    用户名
	 * @return Mes
	 */
	public Mes list(int current, int limit, String name) {
		try {
			Page<AccountEntity> page = new Page<>(current, limit);
			QueryWrapper<AccountEntity> queryWrapper = new QueryWrapper<>();
			if (!StringUtils.isEmpty(name)) {
				queryWrapper.like("name", name);
			}
			queryWrapper.orderByAsc("id");
			mapper.selectPage(page, queryWrapper);
			return Mes.success(page);
		} catch (Exception ex) {
			return Mes.error(ex.getMessage());
		}
	}
}
